thin.imports('/public/configure/conf.bp.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	layer.load(0, {
		shade: [0.08, '#000']
	});

	// 獲取bp列表
	factory.request('_bp_volist');

	$('.bplist').on('click', '.preview', function() {
		var uri = $(this).attr('data-uri');
		tool.__cookie.set('pdf_uri', uri);
		window.open("/pdf.html");
	});

	// btn-PDF
	$('.btn-PDF').on('click', function() {
		$('#uploadfile-pdf').click();
	});

	// 上传商业计划书
	$('#uploadfile-pdf').on('change', function(e) {
		var src,
			url = window.URL || window.webkitURL || window.mozURL,
			files = e.target.files;
		if(!files[0]) return null;
		if(url) {
			src = url.createObjectURL(files[0]);
		} else {
			src = e.target.result;
		}
		var u = $(this).attr('data-u');
		window[u].get($(this), files[0].name);
		var fileInput = this.files[0];
		var form = new FormData();
		form.append("file", fileInput);
		form.append("name", "file");
		form.append("document", "pdf");
		form.append("dir", 'upload');
		ajax().upload('/api?common.upload', form, window[u]);
		this.parentNode.reset();
	});

	$(document).on('click', '#btn-delpdf', function() {
		var _id = $(this).attr('data-id');
		layer.confirm('确定删除商业计划书吗', {
			btn: ['删除', '取消'] //按钮
		}, function(index) {
			//点击确认后的回调函数
			layer.load(0, {
				shade: [0.08, '#000']
			});
			__getdata_bpdelete['id'] = _id;
			factory.request('_bp_delete', _id);
			layer.close(index);
		}, function() {});
	});
});

var __getdata_bpupload = {},
	__getdata_bpdelete = {};

// 獲取bp列表回調
function __complete_bpvolist(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		if(res.data.length > 0) {
			createBP(res.data);
			$('.nothing-box').hide();
		} else {
			$('.bplist').html('');
			$('.nothing-box').show();
		}
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 渲染bp列表
function createBP(data) {
	var ele = '';
	for(var i in data) {
		var list = data[i];
		ele += '<li class="b-line-b clearfix" data-id="' + list.id + '">';
		ele += '<i class="icon-files-pdf fs32r v-middle lhx m-r-6x c-f57c00"></i>';
		ele += list.title;
		ele += '<span class="float-right fs14r c-888">';
		ele += tool.formatDate('yy/mm/dd hh:ii', list.create_time);

		ele += '<a href="javascript:;" id="btn-delpdf" data-id="' + list.id + '">';
		ele += '<i class="icon-delete v-middle lhx fs18r m-l-4x c-3688E9"></i>';
		ele += '</a>';

		ele += '<a href="javascript:;" class="preview" data-uri="' + list.bp_uri + '">';
		ele += '<i class="icon-to_view v-middle lhx fs18r m-l-4x c-3688E9"></i>';
		ele += '</a>';

		ele += '<a href="' + list.bp_uri + '">';
		ele += '<i class="icon-download v-middle lhx fs18r m-l-4x c-3688E9"></i>';
		ele += '</a>';
		ele += '</span>';
		ele += '</li>';
	}
	$('.bplist').html(ele);
}

// 删除商业计划书回调
function __complete_bpdelete(res, unkn) {
	layer.closeAll('loading');
	if(res.status == 204) {
		layer.msg('删除成功', {
			icon: 1,
			time: 1200
		}, function() {
			$('li[data-id=' + unkn + ']').remove();
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 上傳商業計劃書回調
var __ubp = {
	'get': function(obj, name) {
		__getdata_bpupload['title'] = name;
	},
	'onload': function(res) {
		if(res.status == 201) {
			data_bp = res.data.uri;
			layer.load(0, {
				shade: [0.08, '#000']
			});
			__getdata_bpupload['bp_uri'] = res.data.uri;
			factory.request('_bp_upload');
		} else {
			layer.msg(res.error, {
				icon: 2,
				time: 800
			});
		}
	},
	'onerror': function(res) {
		layer.msg('上传失败，请检测网络', {
			icon: 2,
			time: 800
		});
	}
}

// 上傳商業計劃書回調
function __complete_bpupload(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('上传成功', {
			icon: 1,
			time: 1200
		}, function() {
			window.location.reload();
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}